home *** CD-ROM | disk | FTP | other *** search
/ Chip 1996 November / Chip 11-96.iso / workshop / howto / ups < prev    next >
Text File  |  1996-05-25  |  59KB  |  1,590 lines

  1. The UPS HOWTO
  2.    By Harvey J. Stein (hjstein@math.huji.ac.il)
  3.    Copyright (c) 1994 by Harvey J. Stein.  You may use this document
  4.    as you see fit, as long as it remains intact.  In particular, this
  5.    notice (along with the contributions below) must remain untouched.
  6.  
  7. With contributions by (in order of appearance):
  8.    Miquel van Smoorenburg (miquels@cistron.nl.mugnet.org)
  9.    Danny ter Haar (danny@caution.cistron.nl.mugnet.org)
  10.    Hennus Bergman (hennus@sky.nl.mugnet.org)
  11.    Tom Webster (webster@kaiwan.com)
  12.    Marek Michalkiewicz (ind43@sun1000.ci.pwr.wroc.pl)
  13.    Christian G. Holtje (docwhat@uiuc.edu)
  14.    Ben Galliart (bgallia@orion.it.luc.edu)
  15.    Lam Dang (angit@netcom.com)
  16.  
  17. Version 1.2 (last edited Sun Dec 11 1994)
  18.  
  19.                       Contents
  20.  
  21.          0. The usual nonsense
  22.          1. Introduction
  23.          2. What you need to do (summary)
  24.          3. How it's supposed to work
  25.          4. Where to get the appropriate software
  26.          5. How to set things up
  27.      6. User Enhancements
  28.      7. How to make a cable
  29.      8. Serial port pin assignments
  30.      9. Ioctl bit numbers corresponding to RS232 control lines
  31.      10. Info on selected UPSs
  32.          10.0. General Experiences
  33.      10.1. Advice 1200 A
  34.      10.2. GPS
  35.          10.3. TrippLite BC750LAN (Standby UPS)
  36.          10.4. APC Backup-UPS
  37.          10.4.1. A message of caution
  38.          10.4.2. BUPS-HOWTO
  39.          10.5. APC Smart-UPS, Model 600
  40.      11. Reverse-engineering cables & hacking powerd.c
  41.      12. How to shutdown other machines on the same UPS
  42.          12.1. UPS status port method
  43.          12.2. Broadcast method
  44.          12.3. Dummy login method
  45.  
  46.  
  47. 0. The usual nonsense
  48.  
  49. I really can't guarantee that any of this will work for you.
  50. Connecting a UPS to a computer can be a tricky business.  One or the
  51. other or both might burn out, blow up, catch fire, or start World War
  52. Three.  Furthermore, I only have direct experience with the Advice
  53. 1200 A UPS, and I didn't have to make a cable.  So, BE CAREFUL.
  54. GATHER ALL INFORMATION YOU CAN ON YOUR UPS.  THINK FIRST.  DON'T
  55. IMPLICITLY TRUST ANYTHING YOU READ HERE.
  56.  
  57. On the other hand, I managed to get everything working with my UPSes,
  58. without much information from the manufacturer, and without burning
  59. out anything, so it is possible.
  60.  
  61.  
  62. 1. Introduction
  63.  
  64. This HOWTO covers connecting a UPS to a PC running Linux in such a way
  65. that Linux can shutdown cleanly when the power goes out.  To a large
  66. extent it is reduntant, because all the basic info is contained in the
  67. powerd man page that comes with the SysVinit package.  None the less, 
  68. there seems to periodically be alot of discussion on the net regarding
  69. connecting Linux PCs to UPSs, (and the versions of Linux that I
  70. installed didn't come with a powerd man page).  I figured having a
  71. HOWTO would be a good idea because:
  72.  
  73.    -A second source of information might help to understand how
  74.     to connect Linux to a UPS, even if it's just the same information
  75.     written differently.
  76.    -The HOWTO can serve as a repository for UPS specific data.
  77.    -The HOWTO contains additional details that aren't in the powerd
  78.     man page.
  79.  
  80. None the less, this does not replace the powerd man page.  Hopefully,
  81. after reading both, people will be able to deal with UPSs.
  82.  
  83.  
  84. 2. What you need to do (summary)
  85.  
  86.   -Plug the PC into the UPS.
  87.   -Connect the PC's serial port to the UPS with a special cable.
  88.   -Run powerd on the PC.
  89.   -Setup your initd to do something reasonable on powerfail & powerok
  90.    events (like start a shutdown & kill any currently running
  91.    shutdowns, respectively, for example).
  92.  
  93.  
  94. 3. How it's supposed to work
  95.  
  96.    UPS's job:
  97.       When the power goes out, the UPS continues to power the PC &
  98.       signals that the power went out by throwing a relay or turning
  99.       on an opticoupler on it's control port.
  100.  
  101.    Cable's job:
  102.       The cable is designed so that when the UPS throws said relay,
  103.       this causes a particular serial port control line (typically
  104.       DCD) to go high.
  105.  
  106.    Powerd's job:
  107.       Powerd monitors the serial port.  Keeps raised/lowered
  108.       whatever serial port control lines the UPS needs to have
  109.       raised/lowered (typically, DTR must be kept high & whatever line
  110.       shuts off the UPS must be kept low).  When powerd sees the UPS control
  111.       line go high, it writes "FAIL" to /etc/powerfail & sends the
  112.       initd process a SIGPWR signal.  When the control line goes low
  113.       again, it writes "OK" to /etc/powerfail & sends initd a SIGPWR
  114.       signal.
  115.  
  116.    Initd's job (aside from everything else it does):
  117.       When it receives a SIGPWR, it looks at /etc/powerfail.  If it
  118.       contains "FAIL" it runs the powerfail entry from /etc/inittab.
  119.       If it contains "OK" it runs the powerokwait entry from inittab.
  120.  
  121.  
  122. 4. Where to get the appropriate software
  123.  
  124. Pick up /pub/Linux/system/Daemons/SysVinit-2.50.tgz from
  125. sunsite.unc.edu or a mirror.  It includes a copy of powerd.c,
  126. shutdown.c, an initd that understands what to do with SIGPWR, & can
  127. handle powerfail & powerokwait entries in the inittab file.
  128.  
  129.  
  130. 5. How to set things up
  131.  
  132.    -Edit /etc/inittab.  Put in something like this:
  133.  
  134. # What to do when power fails (Halt system & drain battery :):
  135. pf::powerfail:/etc/powerfailscript +5
  136.  
  137. # If power is back before shutdown, cancel the running shutdown.
  138. pg:0123456:powerokwait:/etc/powerokscript
  139.  
  140.    -Write scripts /etc/powerfailscript & /etc/powerokscript to
  141.     shutdown in 5 minutes (or whatever's appropriate) & kill any
  142.     existing shutdown, respectively.  Depending on the version of
  143.     shutdown that you're using, this will be either so trivial that
  144.     you'll dispense with the scripts, or be a 1 line bash script,
  145.     something along the lines of:
  146.  
  147.        kill `ps -aux | grep "shutdown" | grep -v grep | awk '{print $2}'`
  148.  
  149.     and you'll keep the scripts.
  150.  
  151.    -Tell initd to re-process the inittab file with the command:
  152.  
  153.        telinit q
  154.  
  155.    -Edit rc.local so that powerd gets run upon startup.  The syntax
  156.     is:
  157.          powerd <line>
  158.  
  159.     Replace <line> with the serial port that the modem is connected,
  160.     such as /dev/cua1.
  161.  
  162.    -Connect PC's serial port to UPS's serial port.  DO NOT PLUG PC
  163.     INTO UPS YET.
  164.  
  165.    -Plug a light into the UPS.
  166.  
  167.    -Turn on the UPS & the light.
  168.  
  169.    -Run powerd.
  170.  
  171.    -Test the setup:
  172.       -Yank the UPS's plug.
  173.          -Check that the light stays on.
  174.          -Check that /etc/powerfailscript is running.
  175.          -Check that shutdown is running.
  176.       -Plug the UPS back in.
  177.          -Check that the light stays on.
  178.          -Check that /etc/powerfailscript is no longer running.
  179.          -Check that shutdown is no longer running.
  180.       -Yank the UPS's plug again.  Leave it out & make sure that the
  181.        PC shuts down properly in the proper amount of time.
  182.  
  183.       -After everything seems to be proper, powerdown the PC & plug it
  184.        into the UPS.  Run a script that sync's the hard disk every
  185.        second or so.  Simultaneously run a second script that keeps
  186.        doing a find over your entire hard disk.  The first is to make
  187.        this a little safer & the second is to help draw lots of power.
  188.        Now, pull the plug on the UPS, check again that shutdown is
  189.        running & wait.  Make sure that the PC shuts down cleanly
  190.        before the battery on the UPS gives out.
  191.  
  192. Congratulations!  You now have a Linux PC that's protected by a UPS
  193. and will shutdown cleanly when the power goes out!
  194.  
  195.  
  196. 6. User Enhancements
  197.  
  198.    -Hack powerd.c to monitor the line indicating that the batteries
  199.     are low.  When the batteries get low, do an *immediate* shutdown.
  200.    -Modify shutdown procedure so that if it's shutting down in a
  201.     powerfail situation, then it turns off the UPS after doing
  202.     everything necessary.
  203.  
  204.  
  205. 7. How to make a cable
  206.  
  207. This section is just from messages I've seen on the net.  I haven't
  208. done it so I can't write from experience.  If anyone has, please write
  209. this section for me :).  See also the message about the GPS1000
  210. contained in section 10.2.
  211.  
  212.  
  213.    >From miquels@caution.cistron.nl.mugnet.org Wed Jul 21 14:26:33 1993
  214.    Newsgroups: comp.os.linux
  215.    Subject: Re: UPS interface for Linux?
  216.    From: miquels@caution.cistron.nl.mugnet.org (Miquel van Smoorenburg)
  217.    Date: Sat, 17 Jul 93 18:03:37
  218.    Distribution: world
  219.    Organization: Cistron Electronics.
  220.  
  221.    In article <1993Jul15.184450.5193@excaliber.uucp> joel@rac1.wam.umd.edu (Joel M. Hoffman) writes:
  222.    >I'm in the process of buying a UPS (Uninteruptable Power Supply), and
  223.    >notice that some of them have interfaces for LAN's to signal the LAN
  224.    >when the power fails.
  225.    >
  226.    >Is there such an interface for Linux?
  227.    >
  228.    >Thanks.
  229.    >
  230.    >-Joel
  231.    >(joel@wam.umd.edu)
  232.    >
  233.  
  234.    When I worked on the last versioon of SysVinit (Now version 2.4),
  235.    I temporarily had a UPS on my computer, so I added support for it.
  236.    You might have seen that in the latest <signal.h> header files there
  237.    is a #define SIGPWR 30 now :-). Anyway, I did not have such a special
  238.    interface but the output of most UPS's is just a relais that makes or breaks
  239.    on power interrupt. I thought up a simple way to connect this to the
  240.    DCD line of the serial port. In the SysVinit package there is a daemon
  241.    called 'powerd' that keeps an eye on that serial line and sends SIGPWR
  242.    to init when the status changes, so that init can do something (such as
  243.    bringing the system down within 5 minutes). How to connect the UPS to
  244.    the serial line is described in the source "powerd.c", but I will
  245.    draw it here for explanation:
  246.  
  247.             +------------------------o  DTR
  248.             |
  249.               +---+
  250.               |   | resistor
  251.               |   | 10 kilo-Ohm
  252.               |   |
  253.               +---+                                To serial port.
  254.             |
  255.       +-----o-------+------------------------o  DCD
  256.       |             |
  257.       o  UPS        |
  258.     \    relais     |
  259.      \              |
  260.       |             |
  261.       +-----o-------+------------------------o  GND
  262.  
  263.    Nice drawing eh?
  264.  
  265.    Hope this helps.
  266.    SysVinit can be found on sunsite (and tsx-11 probably) as
  267.    SysVinit2.4.tar.z
  268.  
  269.    Mike.
  270.  
  271.    --
  272.  
  273.    Miquel van Smoorenburg, <miquels@cistron.nl.mugnet.org>
  274.    Ibmio.com: cannot open CONFIG.SYS: file handle broke off.
  275.  
  276.  
  277.    >From danny@caution.cistron.nl.mugnet.org Wed Jul 21 14:27:04 1993
  278.    Newsgroups: comp.os.linux
  279.    Subject: Re: UPS interface for Linux?
  280.    From: danny@caution.cistron.nl.mugnet.org (Danny ter Haar)
  281.    Date: Mon, 19 Jul 93 11:02:14
  282.    Distribution: world
  283.    Organization: Cistron Electronics.
  284.  
  285.    In article <9307174330@caution.cistron.nl.mugnet.org> miquels@caution.cistron.nl.mugnet.org (Miquel van Smoorenburg) writes:
  286.    >How to connect the UPS to the serial line is described in the source
  287.    >"powerd.c", but I will draw it here for explanation:
  288.  
  289.    The drawing wasn't really clear, please use this one in stead !
  290.    >
  291.    >                     +------------------------o  DTR
  292.    >                     |
  293.    >                   +---+
  294.    >                   |   | resistor
  295.    >                   |   | 10 kilo-Ohm
  296.    >                   |   |
  297.    >                   +---+                                To serial port.
  298.    >                     |
  299.    >       +-----o-------+------------------------o  DCD
  300.    >       |
  301.    >       o  UPS
  302.    >     \    relais
  303.    >      \
  304.    >       |
  305.    >       +-----o--------------------------------o  GND
  306.    >
  307.  
  308.    The DTR is kept high, when the UPS's power input is gone it
  309.    will close the relais . The computer is monitoring
  310.    the DCD input port to go LOW . When this happens it will start a
  311.    shutdown sequence...
  312.  
  313.    _____
  314.    Danny
  315.  
  316.    --
  317.    <=====================================================================>
  318.    Danny ter Haar  <dannyth@hacktic.nl> or <danny@cistron.nl.mugnet.org>
  319.    Robins law #103: 'a couple of lightyears can't part good friends'
  320.  
  321.  
  322. 8. Serial port pin assignments
  323.  
  324. (The following is from David Tal's <GSRGAAO@TECHNION.BITNET>
  325. 'Frequently Used Cables and Connectors' document).
  326.  
  327.    Pin Assignment for the Serial Port (RS-232C), 25-pin and 9-pin
  328.    --------------------------------------------------------------
  329.    DB-25   DB-9
  330.    Pin #   Pin #   Name    EIA     CCITT   DTE-DCE Description
  331.    -----   -----   -----   -----   -----   ------- -------------------
  332.     1              FG      AA       101    ----    Frame Ground/Chassis GND
  333.     2      3       TD      BA       103    --->    Transmitted Data, TxD
  334.     3      2       RD      BB       104    <---    Received Data, RxD
  335.     4      7       RTS     CA       105    --->    Request To Send
  336.     5      8       CTS     CB       106    <---    Clear To Send
  337.     6      6       DSR     CC       107    <---    Data Set Ready
  338.     7      5       SG      AB       102    ----    Signal Ground, GND
  339.     8      1       DCD     CF       109    <---    Data Carrier Detect
  340.     9              --      --        -       -     Positive DC test voltage
  341.    10              --      --        -       -     Negative DC test voltage
  342.    11              QM      --        -     <---    Equalizer mode
  343.    12              SDCD    SCF      122    <---    Secondary Data Carrier Detect
  344.    13              SCTS    SCB      121    <---    Secondary Clear To Send
  345.    14              STD     SBA      118    --->    Secondary Transmitted Data
  346.    15              TC      DB       114    <---    Transmitter (signal) Clock
  347.    16              SRD     SBB      119    <---    Secondary Receiver Clock
  348.    17              RC      DD       115    --->    Receiver (signal) Clock
  349.    18              DCR     --        -     <---    Divided Clock Receiver
  350.    19              SRTS    SCA      120    --->    Secondary Request To Send
  351.    20      4       DTR     CD       108.2  --->    Data Terminal Ready
  352.    21              SQ      CG       110    <---    Signal Quality Detect
  353.    22      9       RI      CE       125    <---    Ring Indicator
  354.    23              --      CH       111    --->    Data rate selector
  355.    24              --      CI       112    <---    Data rate selector
  356.    25              TC      DA       113    <---    Transmitted Clock
  357.  
  358.     1                         13         1         5
  359.       _______________________________      _______________
  360.       \  . . . . . . . . . . . . .  /      \  . . . . .  /    RS232-connectors
  361.        \  . . . . . . . . . . . .  /        \  . . . .  /     seen from outside
  362.     ---------------------------          -----------      of computer.
  363.     14                      25            6       9
  364.  
  365.    DTE : Data Terminal Equipment (i.e. computer)
  366.    DCE : Data Communications Equipment (i.e. modem)
  367.    RxD : Data received; 1 is transmitted "low", 0 as "high"
  368.    TxD : Data sent; 1 is transmitted "low", 0 as "high"
  369.    DTR : DTE announces that it is powered up and ready to communicate
  370.    DSR : DCE announces that it is ready to communicate; low=modem hangup
  371.    RTS : DTE asks DCE for permission to send data
  372.    CTS : DCE agrees on RTS
  373.    RI  : DCE signals the DTE that an establishment of a connection is attempted
  374.    DCD : DCE announces that a connection is established
  375.  
  376.  
  377.  
  378. 9. Ioctl bit numbers corresponding to RS232 control lines
  379. (taken from /usr/include/linux/termios.h)
  380.  
  381. /* modem lines */
  382. #define TIOCM_LE        0x001
  383. #define TIOCM_DTR       0x002
  384. #define TIOCM_RTS       0x004
  385. #define TIOCM_ST        0x008
  386. #define TIOCM_SR        0x010
  387. #define TIOCM_CTS       0x020
  388. #define TIOCM_CAR       0x040
  389. #define TIOCM_RNG       0x080
  390. #define TIOCM_DSR       0x100
  391. #define TIOCM_CD        TIOCM_CAR
  392. #define TIOCM_RI        TIOCM_RNG
  393.  
  394. Note that the 3rd column is in Hex.
  395.  
  396.  
  397. 10. Info on selected UPSs
  398.  
  399.    **** Please send them to me for inclusion here. ****
  400.  
  401. 10.0. General Experiences.
  402.  
  403. I've been saving peoples comments, but haven't gotten permission yet
  404. to include them here.  Here's a general summary of what I've heard
  405. from people.
  406.  
  407. APC: 
  408.    Won't release info on their "smart" mode without your signature on
  409.    a non-disclosure agreement.  Thus, people are forced to run their
  410.    "Smart" UPSes in the "dumb" mode as outlined above.
  411.  
  412. Tripp Lite:
  413.    One person reported that Tripp lite won't release info either.
  414.  
  415. Upsonic:
  416.    One person reported that Upsonic has discussed technical details
  417.    over the phone, answered questions via faz & are generally helpful.
  418.  
  419.  
  420.  
  421. 10.1. Advice 1200 A
  422.  
  423.    UPS from Advice Electronics, Tel Aviv Israel (they stick their own
  424.    name on the things).
  425.  
  426.    UPS Control Port
  427.    ----------------
  428.  
  429.          2 - Power Fail.
  430.          5 - Battery Low.
  431.          6 - Shut Down UPS.
  432.          4 - Common ground for pin 2, 5, 6.
  433.  
  434.    They also gave me the following picture which didn't help me, but
  435.    may help you if you want to build a cable yourself:
  436.  
  437.  
  438.          2 ----------+
  439.                      |
  440.                      \
  441.                       \|
  442.                        |--------------
  443.                       /|
  444.                     \/     (<--- The "\/" here indicates the type of
  445.                     |             this transister.  I forget what
  446.                     |             denotes what, but this one points
  447.                  +-----+          away from the center line.)
  448.                 /  /  /
  449.  
  450.  
  451.          5 ----------+
  452.                      |
  453.                      \
  454.                       \|
  455.                        |--------------
  456.                       /|
  457.                     \/
  458.                     |
  459.                     |
  460.                  +-----+
  461.                 /  /  /
  462.  
  463.  
  464.                        +-------------
  465.                        |
  466.                        /
  467.               10K    |/
  468.          6 --\/\/\/--|
  469.                      |\
  470.                        \/
  471.                        |
  472.                        |
  473.                     +-----+
  474.                    /  /  /
  475.  
  476.  
  477.          4 ----------+
  478.                      |
  479.                      |
  480.                   +-----+
  481.                  /  /  /
  482.  
  483.  
  484.    Cable supplied
  485.    --------------
  486.    They first game me a cable that was part of a DOS UPS control
  487.    package called RUPS.  I used this for testing.  When I was
  488.    satisfied, they gave me a cable they use for Netware servers
  489.    connected to UPSs.  It functioned identically.  Here are the
  490.    details:
  491.  
  492.       DTR - Powers cable (keep high).
  493.       CTS - Power out (stays high & goes low when power goes out).
  494.       DSR - Battery low (stays high & goes low when battery does).
  495.       RTS - Turns off UPS (keep low & set high to turn off UPS).
  496.  
  497.    (The powerd.c that comes with SysVinit set or left RTS high,
  498.    causing the UPS to shut off immediately when powerd was started
  499.    up!)
  500.  
  501.  
  502. 10.2. GPS1000 from ACCODATA
  503.  
  504.    >From hennus@sky.nl.mugnet.org Thu Mar 10 15:10:22 1994
  505.    Newsgroups: comp.os.linux.help
  506.    Subject: Re: auto-shutdown with UPS
  507.    From: hennus@sky.nl.mugnet.org (Hennus Bergman)
  508.    Date: Tue, 1 Mar 1994 22:17:45 GMT
  509.    Distribution: world
  510.    Organization: The Organization For Removal Of On-Screen Logos
  511.  
  512.    In article <CRAFFERT.94Feb28125452@nostril.lehman.com>,
  513.    Colin Owen Rafferty <craffert@nostril.lehman.com> wrote:
  514.    >I am about to buy an Uninterruptable Power Supply for my machine, and
  515.    >I would like to get one that has the "auto-shutdown" feature.
  516.    >
  517.    I just got one of those real cheap :-)
  518.    It's a GPS1000 by ACCODATA. Anybody know how good the output
  519.    signal of these things is? [Don't have a scope myself :-(]
  520.  
  521.    >I assume that these each have some kind of serial connection that
  522.    >tells the system information about it.
  523.    >
  524.    I took it apart to find out how it worked. There were three optocouplers
  525.    (two output, one input) connected to a 9 pin connector at the back.
  526.    One turns on when the power fails, and goes off again when the power
  527.    returns. While the power is off, you can use the `input' to shut the
  528.    battery off. [It releases the power-relay.] The third one is some kind
  529.    of feedback to tell that it did accepted the `shut-down command'.
  530.    I think the interface for my UPS was designed to be connected to TTL-level
  531.    signals, but with some resistors it could be connected to serial port.
  532.    It's wired in such a way that using a RS-232 port you cannot use both
  533.    output optocouplers; but the shutdown feedback is not necessary anyway,
  534.    just use the important one. ;-)
  535.    [Note that it is possible to blow the transistor part in optocouplers
  536.    with RS-232 levels if you wire it the wrong way round ;-)]
  537.  
  538.    I was hoping I would be able to connect it to my unused game port,
  539.    but that doesn't have an output, does it?
  540.    I'll probably end up getting an extra printer port for this.
  541.  
  542.    Not all UPS' use optocouplers, some use simple relays, which are
  543.    less critical to connect, but of course not as `nice'.
  544.  
  545.    >Has anyone written a package that watches the UPS and does a shutdown
  546.    >(or something) when the power is off?
  547.    SysVinit-2.4 (and probably 2.5 as well) has a `powerd' daemon that
  548.    continually watches a serial port for presence of the CD (Carrier
  549.    Detect) line and signals init when it drops. Init then activates
  550.    shutdown with a time delay. If the power returns within a few minutes
  551.    the shutdown is cancelled. Very Nice.
  552.    The only problem I had with it is that it doesn't actually tell the
  553.    UPS to turn off when the shutdown is complete. It just sits there with
  554.    a root prompt. I'll probably write a small program to shut it down
  555.    >from /etc/brc. RSN.
  556.  
  557.    >    Colin Rafferty, Lehman Brothers <craffert@lehman.com>
  558.  
  559.    Hennus Bergman
  560.  
  561.  
  562. 10.3 TrippLite BC750LAN (Standby UPS)
  563.  
  564. >From webster@kaiwan.com Mon Aug  8 22:30:14 1994
  565. Received: from kaiwan.kaiwan.com by sunset.ma.huji.ac.il with SMTP id AA15677
  566.   (5.65c/HUJI 4.152 for <hjstein@MATH.HUJI.AC.IL>); Mon, 8 Aug 1994 22:28:43 +0300
  567. Received: (webster@localhost) by kaiwan.kaiwan.com (8.6.9/8.6.5) 
  568.       id MAA03960 for hjstein@MATH.HUJI.AC.IL; Mon, 8 Aug 1994 12:26:10 -0700
  569.       *** KAIWAN Internet Access ***
  570. Message-Id: <199408081926.MAA03960@kaiwan.kaiwan.com>
  571. In-Reply-To: <199408051143.AA18932@sunset.ma.huji.ac.il> from "Harvey J. Stein" at Aug 5, 94 02:43:17 pm
  572. X-Mailer: ELM [version 2.4 PL22]
  573. Mime-Version: 1.0
  574. Content-Type: text/plain; charset=US-ASCII
  575. Content-Transfer-Encoding: 7bit
  576. Content-Length: 9535      
  577. From: Tom Webster <webster@kaiwan.com>
  578. To: hjstein@MATH.HUJI.AC.IL (Harvey J. Stein)
  579. Subject: Re: Help - Powerd & UPS - Help
  580. Date: Mon, 8 Aug 1994 12:26:09 -0700 (PDT)
  581.  
  582. Harvey,
  583.  
  584. First off, let me say that I enjoyed reading your HOWTO.  It is 
  585. about what I'd hoped my document might grow into.  I wrote my 
  586. pseudo-HOWTO late on night because I kept seeing the "Can I 
  587. hook a UPS up to Linux...." message, about once a month on 
  588. c.o.l.*.  Mine deals specifically with hooking one vendor's 
  589. model of UPS (Tripplite's BCxxx/LAN series) to a Linux box and 
  590. making powerd work with it.
  591.  
  592. It is in need of some upkeep, things have been a little hectic.  
  593. Now that I have posted it three or four time in response to 
  594. questions, I'm finally getting some feedback (which catches 
  595. these errors).  The problems that I know it has so far are:
  596.  
  597. The proper version of SysVinit is 2.4, not 2.04.
  598.  
  599. There is some argument about whether one or more resistors are 
  600. needed in my cable.  The only place I really see that it might 
  601. be needed is in the inverter shutoff, to make sure that I don't 
  602. send too much voltage to the UPS.  For the sensor circuit I 
  603. don't see why a DTE device can't stand to have its signal 
  604. looped back to it.  All that I'm doing is connecting a line
  605. that is held high to the Carrier Detect line.
  606.  
  607. I should be a little clearer about how the UPS acts when it 
  608. goes into powerfail mode.  The Tripplites provide both an open 
  609. and a closed circuit on powerfail, two different pins and a 
  610. common negative pin.  Thus all I have to do is wire a pin that 
  611. it held high to the carrier detect line and route this through 
  612. the UPS's open on powerfail circuit, to cause the carrier 
  613. detect to drop.  I think that this confuses some people who 
  614. read Miquel van Smoorenburg's description of a UPS that only 
  615. provides only a closed circuit on powerfail, and requires a 
  616. much more complex cable.
  617.  
  618. Well that is all I can think of for now.  I'm planning on 
  619. seeing if my cable will still operate if I insert resistors 
  620. into all of the circuits.  If it does then I'll make the 
  621. changes to my document, should find out this weekend.
  622.  
  623.  
  624. Tom
  625.  
  626. ----------------
  627.  
  628. Of Linux and Uninterruptible Power Supplies
  629. [or How to connect a TrippLite BCXXXLAN UPS]
  630.  
  631. by Tom Webster <webster@kaiwan.com>
  632.    05/20/94 (Version 1.0)
  633.  
  634. 1.0 Introduction
  635. I struggled through connecting a TrippLite BC750LAN (Standby UPS) to
  636. my Linux box about six months ago.  Since then I've seen several
  637. requests for information on this subject, so I'm putting it in a 
  638. relatively stable format so I can just send this out when the 
  639. question reappears.
  640.  
  641. 1.1 The Results
  642. When the power fails in my apartment, several things happen:
  643.  
  644.    1.  The UPS switches its inverter on and the computer starts
  645.        drawing off of the UPS.  The warning beeper also starts going
  646.        off.  If the power comes back on, the UPS shuts the inverter
  647.        down and switches back to line power.  Nothing else happens,
  648.        other than the beeper turns off.
  649.  
  650.    2.  If the power is off for ~15-30 seconds, the system will send
  651.        a message to the users (via wall) and initiate a shutdown 
  652.        (to halt) in five minutes.  If the power comes back on, the 
  653.        shutdown is canceled and a message stating this is sent
  654.        to the users (via wall) stating that the shutdown has been
  655.        canceled.
  656.  
  657.    3.  While the system is shutting itself down, its dying act is to
  658.        shut off the inverter on the UPS, killing power to the system.
  659.        This is done after the disks are unmounted, and is done to
  660.        prevent the halted system from draining power from the UPS.
  661.  
  662.    4.  When the line power comes back on, the system restarts normally. 
  663.  
  664. The BC750 has enough juice to keep my system going for quite some time,
  665. so why do I only run it off of the UPS for five minutes?  The answer
  666. rests in a couple of 'rules-of-thumb' (your mileage may vary):
  667.  
  668.    a.  If the power browns out or blacks out, 90% of the time it will
  669.        be out for 0-2 minutes.
  670.  
  671.    b.  If it is out for longer, it will be out for .5-3+ hours.
  672.  
  673.    c.  If it is out for a while (see b), the power will yo-yo at least
  674.        once while the power company is working on it.  (The power
  675.        will come up for 5-45 seconds, and then fail again.)
  676.  
  677. So, I set my system up to cover the majority of the power outages I
  678. get, without trying to cover the really long ones.  I also keep plenty
  679. of reserve in the UPS to handle yo-yo situations.
  680.  
  681. 1.2  Disclaimer
  682. I make no warranties or guarantees as to the suitability or sanity of
  683. following my advice.  This is how my system is setup, and as far as
  684. I can tell it works fine for me.  Your setup may need to be different
  685. to fit your needs, especially if you are using different UPS hardware.
  686.  
  687. 2.0  Hardware
  688. In the case of my UPS, I thought that the RS-232 interface was something
  689. of a misnomer.  I was expecting the UPS to send and receive data, like
  690. talking to your modem with Hayes "AT" commands.  This was not the case.
  691. It seems that it is called an RS-232 interface because it stays within 
  692. the voltage and signal limits of the RS-232  spec.  To communicate with
  693. the UPS, you need to be able to sense changes in state on certain lines
  694. and change the state of other lines.  The fact that these lines may have
  695. nothing in common to the lines your system might expect to use, if it 
  696. were talking to say a modem or printer, is probably why the UPS 
  697. needs special cables to allow software (including the manufacturer's)
  698. to communicate with the UPS.
  699.  
  700. Through trial and error with a RS-232 patch panel, I was able to come
  701. up with this cable diagram for the cable between the UPS and the 
  702. computer.  Please note that I did this without looking at the official
  703. TrippLite cable and it may be different.
  704.  
  705.           UPS                System
  706.          DB-25               DB-25
  707.            1 <-------------->  1       Ground
  708.  
  709.            2 <-------------->  4       Power Fail
  710.            8 <-------------->  8       Sensing Circuit
  711.  
  712.            3 <-------------->  2       Inverter Shutdown
  713.           20 <--------------> 22       Circuit
  714.  
  715. Once you have the cable patched together, just hook the UPS side to 
  716. the UPS and the System side to a free serial port on your Linux box.
  717. You will probably have to mess around with 9->25 and 25->9 adapters
  718. to get your cable to fit, but you and a good computer store should
  719. be able to handle this.
  720.  
  721. 3.0 Software
  722. The software that I use is all available to Linux users and comes
  723. with most distributions (SLS and Slackware at least).  This setup 
  724. has worked for me through Kernels .99.9, .99.14, and 1.00.
  725.  
  726. 3.1 System V Init
  727. This package is needed to make the whole thing work.  If you are
  728. still using the "Simple Init" package, perhaps it is time you looked
  729. at upgrading.  The version I am using is 2.04, and I believe that
  730. Miquel van Smoorenburg is the author of the package.
  731.  
  732. 3.2 powerd
  733. powerd is the power daemon, by default is sits and watches for a 
  734. change in state on the DCD line and reports these changes to the
  735. system via the signal mechanism.  The source for powerd is provided
  736. in the System V Init package.  Compile it, move it to a binary
  737. directory (I put it in /sbin on my system), and alter your rc.local
  738. script to start the daemon.  The relevant part of my rc.local looks
  739. like this:
  740.  
  741.     ----- snip -----
  742.     # Add support for the UPS
  743.     echo "Starting powerd daemon..."
  744.     if [ -x /sbin/powerd ]; then
  745.        /sbin/powerd /dev/cua4
  746.     fi
  747.     ----- snip -----
  748.  
  749. 3.3 inittab
  750. Your inittab needs to be modified to properly handle the signals
  751. that powerd will send if there is a power failure.  The relevant
  752. lines of my inittab look like this:
  753.  
  754.    ----- snip -----
  755.    # What to do when power fails (shutdown to single user).
  756.    pf::powerfail:/sbin/shutdown -f +5 "THE POWER IS FAILING"
  757.  
  758.    # If power is back before shutdown, cancel the running shutdown.
  759.    pg:0123456:powerokwait:/sbin/shutdown -c "THE POWER IS BACK"
  760.  
  761.    # If power comes back in single user mode, return to multi user mode.
  762.    ps:S:powerokwait:/sbin/init 2
  763.    ----- snip -----
  764.  
  765. 3.4 rc.0 (brc)
  766. Depending on how your system is setup either rc.0 or the brc script
  767. is executed immediately prior to shutdown.  These scripts take care
  768. of things like unmounting disks and any other last minute clean-up.
  769.  
  770. The inverter shutdown circuit, is designed to signal an inverter 
  771. shutdown when data is sent out over the DTR line.  In my case,
  772. I just cat a short file to the serial port (/etc/passwd - since I
  773. know it will always be there).  My rc.0 is as follows, please note
  774. that it is overly conservative, the sync can be removed and the
  775. sleep times can probably be tightened, but it works so I haven't
  776. messed with it.
  777.  
  778.   ----- snip -----
  779.   #! /bin/sh
  780.   #
  781.   # brc       This file is executed by init(8) when the system is being
  782.   #           shutdown (i.e. set to run at level 0).  It usually takes
  783.   #           care of un-mounting all unneeded file systems.
  784.   #
  785.   # Version:  @(#)/etc/brc            2.01    02/17/93
  786.   #
  787.   # Authors:  Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
  788.   #           Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  789.   #
  790.   # Modified: 01/15/94 - Inverter shutdown support added.
  791.   #           Tom Webster <webster@kaiwan.com>
  792.  
  793.   PATH=/sbin:/bin:/usr/sbin:/usr/bin
  794.   echo Unmounting file systems.....
  795.   sync
  796.   umount -a
  797.   sleep 2
  798.   cat /etc/passwd > /dev/cua4
  799.   sleep 5
  800.   echo Done.
  801.   ----- snip -----
  802.  
  803. (On my UPS the inverter is only running when the line power is off, 
  804. so there is no harm in sending the shutdown signal at every shutdown.)  
  805.  
  806. 4.0 Conclusion
  807. Well that's how I hooked my TrippLite UPS up to my Linux box. 
  808. Feel free to drop me a line with the results of you attempts, 
  809. especially if you have any improvements.  :->
  810.  
  811. From: Tom Webster <webster@kaiwan.com>
  812. To: hjstein@MATH.HUJI.AC.IL (Harvey J. Stein)
  813. Subject: Re: Help - Powerd & UPS - Help
  814. Date: Thu, 11 Aug 1994 12:20:50 -0700 (PDT)
  815.  
  816. Harvey,
  817.  
  818. > Like I tried to say, powerd can just run shutdown directly in the
  819. > event of a low battery, so that init doesn't need to deal with it &
  820. > doesn't need to be hacked - no new signal necessary.  Although this
  821. > violates the nice separation of labor between powerd & init, it's
  822. > easier than adding another signal.  Or do you just mean another
  823. > command?  Something like having powerok/powerfail/powerfailnow as
  824. > commands in inittab which execute when SIGPWR is received &
  825. > /etc/powerfail contains OK/FAIL/LOWBATT (respectively).  This would be
  826. > cleanest, but having powerd execute shutdown -r now is trivial to do
  827. > now, and might as well be done - the logic will be the same regardless
  828. > of the action that powerd takes when it senses a low battery.  For now
  829. > it can just run shutdown, & when init gets hacked it can write LOWBATT
  830. > to /etc/powerfail (or whatever the hell the file is called) & give
  831. > init a SIGPWR.
  832.  
  833. I'd like to add the LOWBATT command, it would be the cleaner way to do
  834. it.  I just need to take a look at the code and see how hard it would
  835. be to add it.  Also need to look at my wiring, guess this may mean I've
  836. got to run the system all the way down for a final test (once I think I
  837. have it working.
  838.  
  839. Tom
  840.  
  841.  
  842.  
  843. From: Tom Webster <webster@kaiwan.com>
  844. To: hjstein@MATH.HUJI.AC.IL (Harvey J. Stein)
  845. Subject: Re: Help - Powerd & UPS - Help
  846. Date: Mon, 15 Aug 1994 09:46:06 -0700 (PDT)
  847.  
  848. Harvey,
  849.  
  850. Well, I messed about all weekend taking readings with my multi-
  851. tester and comparing it to the scant documentation that I have for 
  852. the UPS.  The only conclusions I came to were:
  853.  
  854. 1.  My system has been working for about 8-9 months now.  If I was 
  855. going to fry anything, it should have happened by now.
  856.  
  857. 2.  If my success is based on my serial hardware doing odd things 
  858. (I'm using an STB 4COM board), there is no way I'm going to be 
  859. able to find out on my system.  I'll have to leave that to other 
  860. poor souls on the net.
  861.  
  862. 3.  As far as I can tell the only reason the 10kohm resistor was 
  863. in Miquel's diagram, was to keep the line higher than DCD, even 
  864. after the circuit had been shunted to ground.
  865.  
  866. 4.  The only circuit that is expected to do anything other that be 
  867. switched by the UPS (the inverter shutdown circuit) has a 40kohm 
  868. resistor built into it (inside the UPS).  That should take care of 
  869. any worries there.
  870.  
  871. 5.  Miquel's circuit will work for power fail sensing, and might 
  872. be extrapolated to include the low battery circuit.  If my circuit 
  873. proves unviable for others, it would just require more soldering 
  874. than I'd care to deal with in a cable.
  875.  
  876. In other related news, I broke down this morning and ordered a 
  877. cable from Tripplite.  I ordered the LanTastic/LAN Manager/Win NT 
  878. cable, it's just a cable (9M-9F, no software).  This will set me 
  879. back about $40 (the PC UNIX cable (w/ software) is about $140).  
  880. The motivation for doing this was three part.  
  881.  
  882. (1)  Within a year I'll probably be running Win 4.0, or WinNT 3.5 
  883. (I beta'ed 3.1) and both should support UPS monitoring (I know NT 
  884. does and saw the power management icon on a Win4.0 desktop in one 
  885. of the computer mags).  
  886.  
  887. (2)  My current cable isn't going to be compatible with any other 
  888. monitoring software for other OS's (except by random chance), 
  889. working to a known (and presumably soon to be common?) cable via a 
  890. hacked powerd, should widen the audience.  
  891.  
  892. (3)  I can always reverse engineer the cable to see if Tripplite 
  893. is indeed building any safety into their cables.
  894.  
  895. It should be here in a week or so.  In the mean time, I'll start 
  896. looking into hacking powerd for LOWBATT.
  897.  
  898.  
  899. Tom
  900.  
  901.  
  902. 10.4 APC Backup-UPS
  903.  
  904. There seems to be some controversy as to the accuracy of the
  905. information here on APC Back-UPSes.  So, please be careful.  I'm
  906. prefacing this section with one message of caution I received.  It
  907. might not make alot of sense before the rest of this section is read,
  908. but this way, at least you're more likely to see it.  And again, since
  909. I don't have any APC UPS units, I can't verify the accruacy of either
  910. of these messages.
  911.  
  912. 10.4.1. A message of caution
  913.  
  914. >From ind43@sun1000.ci.pwr.wroc.pl Sun Oct  9 11:00:42 1994
  915. Newsgroups: comp.os.linux.admin
  916. Subject: BUPS-HOWTO warning
  917. From: ind43@sun1000.ci.pwr.wroc.pl (Marek Michalkiewicz)
  918. Date: 6 Oct 1994 18:38:15 GMT
  919. Organization: Technical Univeristy of Wroclaw
  920. NNTP-Posting-Host: ci3ux.ci.pwr.wroc.pl
  921. X-Newsreader: TIN [version 1.2 PL2]
  922.  
  923. If you want to connect the APC Back-UPS to your Linux box, this might
  924. be of interest to you.
  925.  
  926. There is a good BUPS-HOWTO which describes how to do this. But it has
  927. one "bug".
  928.  
  929. The RTS serial port signal is used to shut down the UPS. The UPS will
  930. shut down only if it operates from its battery. The manual says that
  931. the shutdown signal must be high for at least 0.5s. But few milliseconds
  932. is enough, at least for my APC Back-UPS 600.
  933.  
  934. Using RTS to shut down the UPS can be dangerous, because the RTS goes
  935. high when the serial device is opened. The backupsd program then turns
  936. RTS off, but it is on (high) for a moment. This kills the power when
  937. backupsd is first started and there is a power failure at this time.
  938. This can happen for example when the UPS is shut down, unattended,
  939. and the power comes back for a while.
  940.  
  941. Either start backupsd before mounting any filesystems for read-write,
  942. or (better) use TX (pin 3) instead of RTS (pin 7) to shut down the
  943. UPS (pin numbers are for 9-pin plug). Use ioctl(fd, TCSBRKP, 10);
  944. to make TX high for one second, for example. Using TX should be safe.
  945. Maybe I will post the diffs if time permits...
  946.  
  947. -- Marek Michalkiewicz
  948. ind43@ci3ux.ci.pwr.wroc.pl
  949.  
  950.  
  951. 10.4.2. BUPS-HOWTO
  952.  
  953.               Luminated Software Group Presents
  954.  
  955.              HOWTO use Back-UPS (by APC)
  956.              (to keep your linux box from frying)
  957.  
  958.  
  959.                 Version: 1.01  BETA
  960.  
  961.  
  962.  
  963.          Document by: Christian G. Holtje <docwhat@uiuc.edu>
  964.     Cabling info and help: Ben Galliart <bgallia@orion.it.luc.edu>
  965.  
  966.  
  967.  
  968.  
  969.     This document, under one condition, is placed in Public Domain. The
  970. one condition is that credit is given where credit is due.  Modify this as
  971. much as you want, just give some credit to us who worked.
  972.  
  973. *******************************************************************************
  974. Warning!
  975.     I, nor any of us who have written or helped with this document, make
  976. and guarantees or claims for this text/source/hints.  If anything is damaged,
  977. we take NO RESPONSIBILITY!  This works to the BEST OF OUR KNOWLEDGE, but
  978. we may have made mistakes.  So be careful!
  979. *******************************************************************************
  980.  
  981.     Al right, you just bought (or are going to buy) a Back-UPS from APC.
  982. (Other brands might be able to use this info, with little or no modification,
  983. but we don't know)  You've looked at the price of the Power-Chute
  984. software/cabling, and just are not sure it's worth the price.  Well, I made my
  985. own cable, and my own software and am using it to automatically shut off the
  986. power to my linux box when a power failure hits.  Guess what?  You can too!
  987.  
  988. *** The Cable ***
  989.  
  990.  
  991.     This was the hardest part to figure out (I know little about hardware,
  992. so Ben did the most work for this).  To build one, you need to buy from your
  993. local radio shack (or other part supplier) this stuff:
  994.     1 9-Position Male D-Subminature Connector (solder-type)
  995.         [Radio Shack cat. no. 276-1537c]
  996.     1 9-Position Female D-Subminature Connector (solder-type)
  997.         [Radio Shack cat. no. 276-1538c]
  998.     2 casings for the above plugs (usually sold separately)
  999.     Some stranded wire (wire made of strands, not solid wire)
  1000.  
  1001. You also need, but may be able to borrow:
  1002.     1 soldering iron
  1003.     solder
  1004.  
  1005. Okay...this is how you connect it up!
  1006.  
  1007. These diagrams are looking into the REVERSE SIDE (the side where you solder
  1008. the wire onto the plugs)  The letters G, R, and B represent the colors of the
  1009. wires I used, and help to distinguish one line from the next.
  1010. (NOTE:  I'm use standard rs-232 (as near as we can tell) numbering.  The APC
  1011. book uses different numbers.  Ignore them!  Use ours...I already changed the
  1012. numbers for you!)
  1013.  
  1014.    ---------------------     Male Side! (This goes into the UPS)
  1015.     \  B   R  *  *  * /     
  1016.       \  *  *  *  G  / 
  1017.         ------------
  1018.  
  1019.  
  1020.    ---------------------     Female Side! (This goes into your COM port)
  1021.     \  R   *  *  *  G /
  1022.       \  *  B  *  *  / 
  1023.         ------------
  1024.  
  1025.  
  1026. For those who like the numbers better:
  1027.     Male        Female
  1028. ---------------------------------------
  1029.     1        7        Black
  1030.     2        1        Red
  1031.     9        5        Green
  1032.  
  1033.  
  1034. ---------Aside:  What the rs-232 pins are for!-----------
  1035. Since we had to dig this info up anyway:
  1036.  
  1037. >From the REAR (the soldering side) the pins are numbered so:
  1038.  
  1039.    ---------------------
  1040.     \  1   2  3  4  5 /
  1041.       \  6  7  8  9  / 
  1042.         ------------
  1043.  
  1044. The pins mean:
  1045.     Number    Name            Abbr. (Sometimes written with D prefix)
  1046.     1    Carrier Detect        CD
  1047.     2    Receive Data        RD
  1048.     3    Transmit Data        TD(?)
  1049.     4    Data Terminal Ready    DTR
  1050.     5    Signal Ground        Gnd
  1051.     6    Data Set Ready        DSR
  1052.     7    Request to Send        RTS(?)
  1053.     8    Clear to Send        CS
  1054.     9    Ring Indicator        RI
  1055.  
  1056. What we did is connect the UPS's RS-232 Line Fail Output to the CD, the UPS's
  1057. chassis to Gnd, and the UPS's RS-232 Shut Down Input to RTS.
  1058. Easy now that we told you, no?
  1059.  
  1060. I have no idea if the software below will work, if you purchase the cable
  1061. from APC.  It might, and it might not.
  1062.  
  1063.  
  1064. *** The Software ***
  1065.  
  1066.     Okay, I use the SysVInit package by Miquel van Smoorenburg for Linux.
  1067. (see end for file locations, credits, email addresses, etc.)  I don't know
  1068. what would have to be changed to use someone elses init, but I know this code
  1069. (following) will work with Miquel's stuff.
  1070.     Just so I give credit where credit's due.  I looked at Miquel's code
  1071. to figure out how ioctl()'s worked.  If I didn't have that example, I'd have
  1072. been in trouble.  I also used the powerfail() routine (verbatim, I think),
  1073. since it must interact with his init, I thought that he should know best.
  1074.     The .c file is at the end of this document, and just needs to be
  1075. clipped off.  To clip the file, edit away and extra '.sigs' and junk.  This
  1076. document should end on the line /* End of File */.....cut the rest.
  1077.  
  1078.     This program can either be run as a daemon to check the status of the
  1079. UPS and report it to init, or it can be run to send the kill-power command
  1080. to the UPS.  The power will only be killed if there is a power problem, and
  1081. the UPS is running off the battery.  Once the power is restored, it turns back
  1082. on.
  1083.  
  1084.     To run as a daemon, just type:
  1085.         backupsd /dev/backups
  1086.  
  1087. /dev/backups is a link to /dev/cua0 at the moment (COM 1, for you DOSers).
  1088. The niceness of the link is that I can just re-link the device if I change
  1089. to com 2 or 3.
  1090.  
  1091. Then, if the power dies init will run the commands for the powerwait.
  1092. An example (This is from my /etc/inittab):
  1093.  
  1094. # Here are the actions for powerfailure.
  1095. pf::powerwait:/etc/rc.d/rc.power start
  1096. po::powerokwait:/etc/rc.d/rc.power stop
  1097.  
  1098. The powerwait will run, if the power goes down, and powerokwait will
  1099. run if the power comes back up.
  1100.  
  1101. Here is my entire rc.power:
  1102. ----------------------------------------------------------------------------
  1103. #! /bin/sh
  1104. #
  1105. # rc.power      This file is executed by init when there is a powerfailure.
  1106. #
  1107. # Version:      @(#)/etc/rc.d/rc.power   1.50    1994-08-10
  1108. #
  1109. # Author:       Christian Holtje, <docwhat@uiuc.edu>
  1110. #
  1111.  
  1112.   # Set the path.
  1113.   PATH=/sbin:/etc:/bin:/usr/bin:/sbin/dangerous
  1114.  
  1115.   # Find out how we were called.
  1116.   case "$1" in
  1117.         start)
  1118.                 echo "Warning there is Power problems."     | wall
  1119.         # Save current Run Level
  1120.         ps | gawk '{ if (($5 == "init") && ($1 == "1")) print $6 }' \
  1121.              | cut -f2 -d[ | cut -f1 -d] \
  1122.              > /tmp/run.level.power
  1123.         /sbin/shutdown -h +1m
  1124.                 ;;
  1125.         stop)
  1126.                 echo "Power is back up.  Attempting to halt shutdown." | wall
  1127.                 shutdown -c
  1128.                 ;;
  1129.         *)
  1130.                 echo "Usage:  $0 [start|stop]"
  1131.                 exit 1
  1132.                 ;;
  1133.   esac
  1134. #End of File
  1135. ------------------------------------------------------------------------------
  1136.  
  1137. Pretty nifty, no?  Actually, there is a problem here...I haven't had
  1138. time to figure it out...If there is a 'sh' wizard out there....
  1139.  
  1140. There is one little detail left, that is having the UPS turn off the power if
  1141. it was halted with the power out.  This is accomplished by adding this line
  1142. into the end of your halt script:
  1143.  
  1144.   /sbin/backupsd /dev/backups killpower
  1145.  
  1146. This will only kill the power if there is no power being supplied to your
  1147. UPS.
  1148.  
  1149.  
  1150. *** Testing the stuff ***
  1151.  
  1152.     This is just a short section saying this:
  1153.  
  1154.     BE CAREFUL!
  1155.  
  1156.     I recommend backing up your linux partitions, syncing several times
  1157. before testing and just being careful in general.  Of course, I'm just
  1158. recommending this.  I wasn't careful at all, and had to clean my partition
  1159. several times testing my config.  But it works.  :)
  1160.  
  1161.  
  1162. *** Where to Get It ***
  1163.  
  1164.     Miquel van Smoorenburg's SysVInit can be gotten at:
  1165.     sunsite.unc.edu:/pub/Linux/system/Daemons/SysVinit-2.50.tgz
  1166.  
  1167.     and a fix for some bash shells is right next-door as:
  1168.     sunsite.unc.edu:/pub/Linux/system/Daemons/SysVinit-2.50.patch1
  1169.     
  1170.     As to getting this HOWTO, you can email me.
  1171.     docwhat@uiuc.edu  with the subject saying 'request'
  1172.     and the keyword 'backups' in body of the letter.
  1173.     (I may automate this, and other stuff)
  1174.  
  1175. *** Credit Where Credit's Due Dept. ***
  1176.  
  1177.     Thanks to Miquel van Smoorenburg <miquels@drinkel.nl.mugnet.org>
  1178. for his wonderful SysVInit package and his powerd.c which helped me very much.
  1179.  
  1180.     Christian Holtje <docwhat@uiuc.edu>
  1181.         Documentation
  1182.         backupsd.c (what wasn't Miquel's)
  1183.         rc.power
  1184.  
  1185.     Ben Galliart <bgallia@orion.it.luc.edu>
  1186.         The cable
  1187.         Information for the RS-232 standard
  1188.         Lousy Jokes (none quoted here)
  1189.  
  1190. ------------------>8-------------CUT HERE--------8<---------------------------
  1191.  
  1192.  
  1193.  
  1194.  
  1195.  
  1196. /*  backupsd.c -- Simple Daemon to catch power failure signals from a
  1197.  *                Back-UPS (from APC).
  1198.  * 
  1199.  *  Parts of the code are from Miquel van Smoorenburg's powerd.c
  1200.  *  Other parts are original from Christian Holtje <docwhat@uiuc.edu>
  1201.  *  I believe that it is okay to say that this is Public Domain, just
  1202.  *  give credit, where credit is due.
  1203.  *
  1204.  *  Disclaimer:  We make NO claims to this software, and take no
  1205.  *               resposibility for it's use/misuse.
  1206.  */
  1207.  
  1208. #include <sys/types.h>
  1209. #include <sys/ioctl.h>
  1210. #include <fcntl.h>
  1211. #include <errno.h>
  1212. #include <stdlib.h>
  1213. #include <unistd.h>
  1214. #include <stdio.h>
  1215. #include <signal.h>
  1216.  
  1217. /* This is the file needed by SysVInit */
  1218. #define PWRSTAT         "/etc/powerstatus"
  1219.  
  1220. void powerfail(int fail);
  1221.  
  1222. /* Main program. */
  1223. int main(int argc, char **argv)
  1224. {
  1225.   int fd;
  1226.   int killpwr_bit = TIOCM_RTS;
  1227.   int flags;
  1228.   int status, oldstat = -1;
  1229.   int count = 0;
  1230.  
  1231.   if (argc < 2) {
  1232.         fprintf(stderr, "Usage: %s <device> [killpower]\n", argv[0]);
  1233.         exit(1);
  1234.   }
  1235.  
  1236.   /* Open the the device */
  1237.   if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
  1238.         fprintf(stderr, "%s: %s: %s\n", argv[0], argv[1], sys_errlist[errno]);
  1239.         exit(1);
  1240.   }
  1241.  
  1242.   if ( argc >= 3  && (strcmp(argv[2], "killpower")==0) )
  1243.       {
  1244.       /* Let's kill the power! */
  1245.       fprintf(stderr, "%s: Attempting to kill the power!\n",argv[0] );
  1246.       ioctl(fd, TIOCMBIS, &killpwr_bit); 
  1247.       /* Hmmm..... If you have a power outtage, you won't make it! */
  1248.       exit(0);
  1249.       }
  1250.   else
  1251.       /* Since we don't want to kill the power, clear the RTS. (killpwr_bit) */
  1252.       ioctl(fd, TIOCMBIC, &killpwr_bit); 
  1253.  
  1254. /* Become a daemon. */
  1255.   switch(fork()) {
  1256.   case 0: /* I am the child. */
  1257.                 setsid();
  1258.                 break;
  1259.   case -1: /* Failed to become daemon. */
  1260.                 fprintf(stderr, "%s: can't fork.\n", argv[0]);
  1261.                 exit(1);
  1262.   default: /* I am the parent. */
  1263.                 exit(0);
  1264.   }
  1265.  
  1266.  
  1267.   /* Now sample the DCD line. */
  1268.   while(1) {
  1269.       ioctl(fd, TIOCMGET, &flags);
  1270.       status = (flags & TIOCM_CD); 
  1271.       /* Did DCD jumps to high? Then the power has failed. */
  1272.       if (oldstat == 0 && status != 0) {
  1273.       count++;
  1274.       if (count > 3) powerfail(0);
  1275.       else { sleep(1); continue; }
  1276.       }
  1277.       /* Did DCD go down again? Then the power is back. */
  1278.       if (oldstat > 0 && status == 0) {
  1279.       count++;
  1280.       if (count > 3) powerfail(1);
  1281.       else { sleep(1); continue; }
  1282.       }
  1283.       /* Reset count, remember status and sleep 2 seconds. */
  1284.       count = 0;
  1285.       oldstat = status;
  1286.       sleep(2);
  1287.   }
  1288.   /* Error! (shouldn't happen) */
  1289.   return(1);
  1290. }
  1291.  
  1292.  
  1293. /* Tell init the power has either gone or is back. */
  1294. void powerfail(ok)
  1295. int ok;
  1296. {
  1297.   int fd;
  1298.  
  1299.   /* Create an info file needed by init to shutdown/cancel shutdown */
  1300.   unlink(PWRSTAT);
  1301.   if ((fd = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
  1302.         if (ok)
  1303.                 write(fd, "OK\n", 3);
  1304.         else
  1305.                 write(fd, "FAIL\n", 5);
  1306.         close(fd);
  1307.   }
  1308.   kill(1, SIGPWR);
  1309. }
  1310.  
  1311. /* End of File */
  1312.  LocalWords:  rc
  1313.  
  1314. 10.5. APC Smart-UPS, Model 600
  1315.  
  1316. Many people have APC Smart UPSes.  To the best of my knowledge, no one
  1317. can run them in "smart" mode under Linux.  This is because APC refuses
  1318. to release the protocol for the "smart" mode without a non-disclosure
  1319. agreement.  Not very smart of them, I'd say :).
  1320.  
  1321. The general consensus is to buy from a brand which does release the
  1322. information.  Best is one such brand.
  1323.  
  1324. If you are stuck with an APC Smart-UPS, you can still use it, but only
  1325. in a dumb mode like all the other UPSes & as outlined above.
  1326.  
  1327. Here's some info on how to make a cable for doing such.  You'll
  1328. probably have to hack powerd.c as outlined in section 11.
  1329.  
  1330.  
  1331. >From dangit@netcom.com Mon Aug 22 10:16:23 1994
  1332. Newsgroups: comp.os.linux.misc
  1333. Subject: UPS Monitoring Cable For APC
  1334. From: dangit@netcom.com (Lam Dang)
  1335. Date: Fri, 19 Aug 1994 11:56:28 GMT
  1336. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  1337. X-Newsreader: TIN [version 1.2 PL1]
  1338.  
  1339. [Didn't make it the first time.]
  1340.  
  1341. A few netters have asked about UPS monitoring cables.  This is what I
  1342. found when I made one for my APC Smart-UPS, Model 600.  A disclaimer is in
  1343. order.  This is just an experimenter's report; use it at your own risks.
  1344. Please read the User's Manual first, especially Section 6.4, Computer
  1345. Interface Port.
  1346.  
  1347. The cable is to run between a 9-pin female connector on the UPS and a
  1348. 25-pin male connector on the PC.  Since I cut off one end of a 9-pin
  1349. cable and replaced it with a 25-pin connector, I had to be VERY
  1350. CAREFUL ABOUT PIN NUMBERS.  The 25-pin hood is big enough to contain a
  1351. voltage regulator and two resistors.  I got all the materials (listed
  1352. below) from Radio Shack for less than 10 bucks.  As required by Windows NT
  1353. Advanced Server 3.5 (Beta 2), the "interface" between the UPS connector
  1354. and the PC connector is as follows:
  1355.  
  1356.         UPS (9-pin)          PC (25-pin)
  1357.  
  1358.         1 (Shutdown)         20 (DTR)
  1359.         3 (Line Fail)          5 (CTS)
  1360.         4 (Common)           7 (GND)
  1361.         5 (Low Battery)            8 (DCD)
  1362.         9 (Chassis Ground)      1 (Chassis Ground)
  1363.  
  1364. This is pretty straightforward.  You can use UPS pin 6 instead of 3
  1365. (they're the inverse of each other).  The complication is in pulling up
  1366. UPS open collector pins 3 (or 6) and 5.
  1367.  
  1368. This APC model provides an unregulated output of 24 Vdc at UPS pin 8. The
  1369. output voltage is available all the time (at least until some time after
  1370. Low Battery has been signalled).  The supply is limited to 40 mA.  To
  1371. pull up, UPS pin 8 is input to a +5 Vdc voltage regulator.  The output of
  1372. the regulator goes into two 4.7K resistors.  The other end of one
  1373. resistor connects both UPS pin 3 (Line Fail) and PC pin 5 (CTS).  That
  1374. of the other resistor connects both UPS pin 5 (Low Battery) and PC pin 8
  1375. (DCD).  The two resistors draw about 2 mA when closed.
  1376.  
  1377. Test your cable without connecting it to the PC.  When the UPS is on
  1378. line, pins 5 (CTS) and 8 (DCD) at the PC end of the cable should be very
  1379. close to 5 Vdc, and applying a high to pin 20 (DTR) for 5 seconds should
  1380. have no effect.  Now pull the power plug to put the UPS on battery.  Pin
  1381. 5 (CTS) should go down to zero Vdc, pin 8 (DCD) should stay the same at 5
  1382. Vdc, and applying a high to pin 20 (DTR), e.g., by shorting pins 8 and 20,
  1383. should shut down the UPS after about 15 seconds.
  1384.  
  1385. Keep the UPS on battery until Low Battery is lighted on its front panel.
  1386. Now pin 8 (DCD) should go down to zero Vdc too.  Wait until the UPS
  1387. battery is recharged.  Then connect your cable to the PC, disable the UPS
  1388. option switches by turning all of them ON, and run your favorite UPS
  1389. monitoring software.
  1390.  
  1391. For those who want to run it with Windows NT Advanced Server, the UPS
  1392. interface voltages are NEGATIVE for both power failure (using UPS pin 3)
  1393. and low battery conditions, and POSITIVE for remote shutdown.  Serial
  1394. line parameters such as baud rate don't matter.
  1395.  
  1396. I haven't tested my cable with Linux powerd.  When you do, please let us
  1397. know.  I run NT as often as Linux on the same PC.  I must conform to NT's
  1398. UPS scheme.  Perhaps somebody can modify powerd to work with it and post
  1399. the source code here.
  1400.  
  1401. List of materials:
  1402.  
  1403.         1 shielded D-sub connector hood (Radio Shack 276-1510)
  1404.         1 25-pin female D-sub crimp-type connector (276-1430)
  1405.         1 7805 +5Vdc voltage regulator (276-1770)
  1406.         2 4.7K resistors
  1407.         1 component perfboard (276-148)
  1408.         1 cable with at least one 9-pin male connector.
  1409.  
  1410. You'll need a multimeter, a soldering iron, and a couple of hours.
  1411.  
  1412. Hope this helps.
  1413.  
  1414. Regards,
  1415.  
  1416. --
  1417. Lam Dang
  1418. dangit@netcom.com
  1419.  
  1420.  
  1421. 11. Reverse-engineering cables & hacking powerd.c
  1422.  
  1423. Try to get documentation for the cables that your UPS seller supplies.
  1424. In particular find out:
  1425.  
  1426.    -What lines need to be kept high.
  1427.    -What line(s) turn off the UPS.
  1428.    -What lines the UPS toggles to indicate that:
  1429.       -Power is out.
  1430.       -Battery is low.
  1431.  
  1432. You then need to hack powerd.c appropriately.
  1433.  
  1434. If you have trouble getting the above information (or just want to
  1435. check it) the following program (upscheck.c) might help.  It's a
  1436. hacked version of powerd.c.  It allows you to set the necessary port
  1437. flags from the command line & then monitors the port, displaying the
  1438. control lines every second.  I used it as "upscheck /dev/cua1 2" (for
  1439. example) to set the 2nd bit (DTR) & to clear the other bits.  The
  1440. number base 2 indicates which bits to set, so for example to set bits
  1441. 1, 2 & 3, (& clear the others) use 7.  See the code for details.
  1442.  
  1443. Here's the (untested) upscheck.c program.  It's untested because I
  1444. edited the version I originally used to make it clearer, and can't
  1445. test the new version at the moment.
  1446.  
  1447.  --------- Begin upscheck.c ------------------
  1448. /*
  1449.  * upscheck    Check how UPS & computer communicate.
  1450.  *
  1451.  * Usage:    upscheck <device> <bits to set>
  1452.  *              For example, upscheck /dev/cua4 4 to set bit 3 &
  1453.  *              monitor /dev/cua4.
  1454.  *
  1455.  * Author:    Harvey J. Stein <hjstein@math.huji.ac.il>
  1456.  *              (but really just a minor modification of Miquel van
  1457.  *              Smoorenburg's <miquels@drinkel.nl.mugnet.org> powerd.c
  1458.  *
  1459.  * Version:    1.0 19940802
  1460.  *
  1461.  */
  1462. #include <sys/types.h>
  1463. #include <sys/ioctl.h>
  1464. #include <fcntl.h>
  1465. #include <errno.h>
  1466. #include <stdlib.h>
  1467. #include <unistd.h>
  1468. #include <stdio.h>
  1469. #include <signal.h>
  1470.  
  1471. /* Main program. */
  1472. int main(int argc, char **argv)
  1473. {
  1474.   int fd;
  1475.  
  1476. /*  These TIOCM_* parameters are defined in <linux/termios.h>, which  */
  1477. /*  is indirectly included here.                                      */
  1478.   int dtr_bit = TIOCM_DTR;
  1479.   int rts_bit = TIOCM_RTS;
  1480.   int set_bits;
  1481.   int flags;
  1482.   int status, oldstat = -1;
  1483.   int count = 0;
  1484.   int pc;
  1485.  
  1486.   if (argc < 2) {
  1487.     fprintf(stderr, "Usage: upscheck <device> <bits-to-set>\n");
  1488.     exit(1);
  1489.   }
  1490.  
  1491.   /* Open monitor device. */
  1492.   if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
  1493.     fprintf(stderr, "upscheck: %s: %s\n", argv[1], sys_errlist[errno]);
  1494.     exit(1);}
  1495.  
  1496.   /* Line is opened, so DTR is high. Force it anyway to be sure. */
  1497.   /*    ioctl(fd, TIOCMBIS, &dtr_bit); */
  1498.   /*    The above line was from the original powerd.c, but it turned off */
  1499.   /*    my UPS!  So, I changed it to the line below which clears the DTR */
  1500.   /*    instead of setting the DTR bit & that worked for me.  However,   */
  1501.   /*    it might not work for you, so I commented it out too.            */
  1502.   /*    ioctl(fd, TIOCMBIC, &dtr_bit); */
  1503.  
  1504.   /* Get the bits to set from the command line. */
  1505.   sscanf(argv[2], "%d", &set_bits);
  1506.  
  1507.   while (1) {
  1508.     /* Set the command line specified bits (& only the command line */
  1509.     /* specified bits).                                             */
  1510.     ioctl(fd, TIOCMSET, &set_bits);
  1511.     fprintf(stderr, "Setting %o.\n", set_bits);
  1512.  
  1513.     sleep(1);
  1514.  
  1515.     /* Get the current line bits */
  1516.     ioctl(fd, TIOCMGET, &flags);
  1517.     fprintf(stderr, "Flags are %o.\n", flags);
  1518.  
  1519. /*  Fiddle here by changing TIOCM_CTS to some other TIOCM until    */
  1520. /*  this program detects that the power goes out when you yank     */
  1521. /*  the plug on the UPS.  Then you'll know how to modify powerd.c. */
  1522.     if (flags & TIOCM_CTS) 
  1523.       {
  1524.     pc = 0 ;
  1525.     fprintf(stderr, "power is up.\n");
  1526.       }
  1527.     else 
  1528.       { 
  1529.     pc = pc + 1 ; 
  1530.     fprintf(stderr, "power is down.\n");
  1531.       }
  1532.     }
  1533.  
  1534.   close(fd);
  1535. }
  1536.  ----------- End upscheck.c ---------------------------
  1537.  
  1538.  
  1539.  
  1540. 12. How to shutdown other machines on the same UPS
  1541.  
  1542. Some people (myself included) have several Linux PCs connected to one
  1543. UPS.  One PC monitors the UPS & needs to get the other PCs to shut
  1544. down when the power goes out.  There are a number of ways to do this,
  1545. all are do-it-yourself currently, and most are just hypothetical.
  1546.  
  1547. We assume the PCs can communicate over a network.  Call the PC that
  1548. monitors the UPS the master & the other PCs the slaves.
  1549.  
  1550.  
  1551. 12.1. UPS status port method
  1552.  
  1553. Set up a port on the master which, when connected to, either sends
  1554. "OK", "FAIL", or "BATLOW", the first when the power is ok, the second
  1555. when the power has failed, and the third when the battery is low.
  1556. Model this on port 13 (the time port) which one can telnet to &
  1557. receive the local time.
  1558.  
  1559. Have the slaves run versions of powerd that look at this port instead
  1560. of checking a serial line.
  1561.  
  1562. The only down side I can see to this method is the network load due to
  1563. checking this port.  One would want to check this port often to
  1564. quickly catch the BATLOW message & shut down before the battery dies.
  1565.  
  1566.  
  1567. 12.2. Broadcast method
  1568.  
  1569. Same as 12.1 except send an ethernet broadcast message that the power
  1570. has just gone down.
  1571.  
  1572. This might have security implications.
  1573.  
  1574.  
  1575. 12.3. Dummy login method
  1576.  
  1577. Set up dummy logins on the slaves with login names "powerok" &
  1578. "powerfail", both with the same UID.  Make /etc/powerokscript the
  1579. shell of the powerok user, & make /etc/powerfailscript the shell of
  1580. the powerfail user.  On the master, have the /etc/powerokscript rlogin
  1581. to each slave as user powerok, & have the /etc/powerfailscript rlogin
  1582. to each slave as user powerfail.  Put a .rhosts file on each slave in
  1583. the home directory of powerok & powerfail to allow root from the
  1584. master to login as user powerok & powerfail to each slave.
  1585.  
  1586. This is the system I'm currently using.
  1587.  
  1588. This might also have security implications.
  1589.  
  1590.